#!/bin/bash
# Program: 
#    Git smart tag.
#
# Copyright (C) Lo Chia Chi
#
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# History:
# 2012/03/09 first version.
#            lo.chiachi@gmail.com
#            jack989_lo@compalcomm.com


# small build version.
#smart-gat.sh "TEST" "small"
##TEST.000.000
# TEST.000.001
##TEST.001.000
# TEST.001.001


# big build version.
#smart-git.sh "TEST" "big"
# when fix or small version number not zone, then add big version number.
##TEST.000.xxx
# TEST.001.000
##TEST.001.001
# TEST.002.000


LOG_INFO="1"
LOG_DEBUG="0"
LOG_ERORR="1"
LOG_FUNCTION="0"


function logd ()
{
  if [ "${LOG_DEBUG}" == "1" ]; then
    echo "$@" | sed "s/^/D:/g"
  fi
}

function loge ()
{
  if [ "${LOG_ERORR}" == "1" ]; then
    echo "$@" | sed "s/^/E:/g"
  fi
}

function logi ()
{
  if [ "${LOG_INFO}" == "1" ]; then
    echo "$@" | sed "s/^/I:/g"
  fi
}

function logf ()
{
  if [ "${LOG_FUNCTION}" == "1" ]; then
    echo "$@" | sed "s/^/FUNC:/g"
  fi
}

function failed ()
{
  loge "$1: exit code $2"
  exit $2
}


function find_project_root()
{
  ORG_PWD=`pwd`
  export PROJ_DIR=""
  logf "find_project_root ORG_PWD:$ORG_PWD"

  COUNT=10
  FIND_DIR=0
  
  while [ $COUNT -gt 0 ]; do 

    if [ -d ".repo" ]; then
      FIND_DIR=1
      export PROJ_DIR=`pwd`
      break;
    fi
    COUNT=$(($COUNT - 1))
    cd ..
  done

  cd $ORG_PWD

  if [ $FIND_DIR -eq 1 ]; then
    logi "  have find project folder: $PROJ_DIR"
    
    return 0
  fi

  logi "  can not have find project folder\n"
  return 1
}

#REPO_PATH
function report()
{
  new_version=$1
  old_version=$2
  find_project_root
  out_file="$PROJ_DIR/$3"
  logf "Report: new_version: ${new_version}"
  logf "        old_version: ${old_version}"
  logf "        out_file:    ${out_file}"
  

  echo "=====================================================" >> ${out_file}
  git_remote_project=`git config remote.korg.projectname`
  my_path=`pwd`
  if [ "${REPO_PATH}" != "" ]; then
    echo "Project path:${REPO_PATH}" >> ${out_file}
  elif [ "${git_remote_project}" != "" ]; then
    echo "Project path:${git_remote_project}" >> ${out_file}
  else
    echo "Project path:${my_path}" >> ${out_file}
  fi

  echo "New Version: ${new_version} Old Version:${old_version}"  >> ${out_file}
  git log --stat ${old_version}..${new_version} | sed "s/^/  /g"  >> ${out_file}
  echo "" >> ${out_file}
  echo "" >> ${out_file}
}

function main()
{
  version_title="$1"
  version_type="$2"
  if [ "$#" == "3" ]; then
    report_file_name=$3
    logf "Main: Version Title: ${version_title}"
    logf "      Version type:  ${version_type}"
    logf "      Output file:   ${report_file_name}"
  else
    logf "Main: Version Title: ${version_title}"
    logf "      Version type: ${version_type}"
    report_file_name="version_report.txt"
  fi
  

  last_version_list=`git tag | grep -E "^${version_title}\.[0-9][0-9][0-9]\.[0-9][0-9][0-9]"`
  last_version_ret=$?
  logd "last_version_list:"
  logd "$last_version_list"

  if [ "$last_version_ret" != "0" ]; then
    git tag "${version_title}.000.000"
    logi "建立初始TAG:${version_title}.000.000"
    exit 0
  fi

  last_version=`git tag | grep -E "^${version_title}\.[0-9][0-9][0-9]\.[0-9][0-9][0-9]" | sort | tail -n 1`
  logd "last_version:${last_version}"

  big_version_id=`echo ${last_version} | cut -d. -f 2`
  small_version_id=`echo ${last_version} | cut -d. -f 3`

  logd "取得 big version:${big_version_id} small version:${small_version_id}"
  
  last_commit_id=`git log --shortstat --no-color -1 ${last_version} | grep -E "^commit" | sed "s/.* //g"`
  head_commit_id=`git log --shortstat --no-color -1 | grep "^commit" | sed "s/.* //g"`
  logd "last_commit_id: ${last_commit_id}"
  logd "head_commit_id: ${head_commit_id}"

  if [ "${last_commit_id}" == "${head_commit_id}" ]; then
    logi "目前版本TAG為:${last_version}"
  fi

  
  case "$version_type" in
    "small")
      if [ "${last_commit_id}" != "${head_commit_id}" ]; then
        new_small_version=$((${small_version_id}+1))
        new_small_version=`echo "${new_small_version}" | awk '{printf "%03d\n", $1}'`
        new_version_tag="${version_title}.${big_version_id}.${new_small_version}"
        logi "Add small version TAG:${new_version_tag}"
        #git tag -a "${new_version_tag}" -m "Auto add small version."
        git tag "${new_version_tag}"
        report ${new_version_tag} ${last_version} ${report_file_name}
      else
        logi "版本一樣，不需要加TAG."
      fi
      ;;

    "big")
      if [ "${small_version_id}" != "000" ] || [ "${last_commit_id}" != "${head_commit_id}" ]; then
        new_big_version=`echo $(( ${big_version_id} + 1 )) | awk '{printf "%03d\n", $1}'`
        new_version_tag="${version_title}.${new_big_version}.000"
        logi "Add big version TAG:${new_version_tag}"
        git tag "${new_version_tag}"
        report ${new_version_tag} ${last_version} ${report_file_name}
      else
        logi "版本一樣，不需要加TAG."
      fi
      ;;

  esac
}

main $@


